<template>
    <div class="tab-content">
        <el-form ref="revenueFormRef" :model="revenueForm" :rules="revenueRules" label-width="150px" class="config-form">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="今日共享收益" prop="todaySharedRevenue">
                        <el-input v-model="revenueForm.todaySharedRevenue" placeholder="请输入今日共享收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="本月共享收益" prop="monthSharedRevenue">
                        <el-input v-model="revenueForm.monthSharedRevenue" placeholder="请输入本月共享收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="今日外卖收益" prop="todayTakeawayRevenue">
                        <el-input v-model="revenueForm.todayTakeawayRevenue" placeholder="请输入今日外卖收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="本月外卖收益" prop="monthTakeawayRevenue">
                        <el-input v-model="revenueForm.monthTakeawayRevenue" placeholder="请输入本月外卖收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="今日民用收益" prop="todayCivilRevenue">
                        <el-input v-model="revenueForm.todayCivilRevenue" placeholder="请输入今日民用收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="本月民用收益" prop="monthCivilRevenue">
                        <el-input v-model="revenueForm.monthCivilRevenue" placeholder="请输入本月民用收益">
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div class="save-button-container">
            <el-button type="primary" @click="saveRevenueData">保存收费数据配置</el-button>
        </div>
    </div>
</template>
<script lang="ts" setup>
    import { ref, reactive } from 'vue'
    import { updateDataClassify } from '@/api/port'
    import { ElMessage } from 'element-plus'

    // 模块6: 收费数据配置表单
    const revenueRules = {
        todaySharedRevenue: [{ required: true, message: '今日共享收益不能为空', trigger: 'blur' }],
        monthSharedRevenue: [{ required: true, message: '本月共享收益不能为空', trigger: 'blur' }],
        todayTakeawayRevenue: [{ required: true, message: '今日外卖收益不能为空', trigger: 'blur' }],
        monthTakeawayRevenue: [{ required: true, message: '本月外卖收益不能为空', trigger: 'blur' }],
        todayCivilRevenue: [{ required: true, message: '今日民用收益不能为空', trigger: 'blur' }],
        monthCivilRevenue: [{ required: true, message: '本月民用收益不能为空', trigger: 'blur' }]
    }
    const revenueForm = ref({
        todaySharedRevenue: 0, // 今日共享收益
        monthSharedRevenue: 0, // 本月共享收益
        todayTakeawayRevenue: 0, // 今日外卖收益
        monthTakeawayRevenue: 0, // 本月外卖收益
        todayCivilRevenue: 0, // 今日民用收益
        monthCivilRevenue: 0 // 本月民用收益
    })
    const revenueFormRef = ref()
    const saveRevenueData = async () => {
        if (!revenueFormRef.value) return
        
        try {
            await revenueFormRef.value.validate()
            await updateDataClassify({
                id: 4,
                key: 'module6',
                value: revenueForm.value
            })
            console.log('保存收费数据:', revenueForm.value)
            ElMessage.success('收费数据保存成功')
        } catch (error) {
            ElMessage.warning('请填写完整的表单信息')
        }
    }
    
    // 初始化函数
    const init = (module: any) => {
        console.log(module)
        if(module){
            revenueForm.value = module
        }else{
            revenueForm.value = {
                todaySharedRevenue: 0, // 今日共享收益
                monthSharedRevenue: 0, // 本月共享收益
                todayTakeawayRevenue: 0, // 今日外卖收益
                monthTakeawayRevenue: 0, // 本月外卖收益
                todayCivilRevenue: 0, // 今日民用收益
                monthCivilRevenue: 0 // 本月民用收益
            }
        }
    }
    // 暴露方法给父组件
    defineExpose({
        init
    })
</script>
<style lang="less" scoped>

</style>